<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchList">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="机构名称">
              <j-select-depart
                v-model="queryParam.officeId"
                :trigger-change="true"
                customReturnField="id"
                :multi="false"
              ></j-select-depart>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="年度">
              <a-date-picker
                mode="year"
                placeholder="请选择年份"
                format="YYYY"
                value-format="yyyy-MM-dd"
                v-model="queryParam.year"
                :open="yearPickShow"
                style="width: 100%"
                @panelChange="handlePanelChange"
                @openChange="handleOpenChange"
              />
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-item label="季度">
                <j-dict-select-tag
                  style="width: 100%"
                  v-model="queryParam.quarter"
                  placeholder="请选择"
                  dict-code="sys_quarter"
                />
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchList" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
              <a-button
                type="primary"
                style="
                  padding: 0 10px;
                  margin-left: 10px;
                  background-color: #1ea489 !important;
                  border: 1px solid #1ea489 !important;
                "
                @click="mergeReports"
              >合并报表</a-button
              >
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columnsTransfer"
        :dataSource="dataSource"
        :pagination="ipagination"
        :row-selection="rowSelection"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="departName" slot-scope="departName">
          <div style="width: 100%; height: 35px; line-height: 35px; overflow-x: hidden; overflow-y: auto">
            {{ departName }}
          </div>
        </template>
        <span slot="actionName">
          <a-button size="small" @click="openCreatModel" v-has="'report:add'" type="primary" icon="plus">新增</a-button>
        </span>
        <span slot="create" slot-scope="text, record">
          {{ record.createByText || record.realName }}
        </span>
        <span slot="ifstate" slot-scope="text, record">
          {{ record.status_dictText || record.isMerge_dictText }}
        </span>
        <span slot="applystate" slot-scope="text, record">
          {{ record.applystate_dictText || record.state_dictText }}
        </span>
        <span slot="action" slot-scope="text, record">
          <span v-has="'report:detail'">
            <a @click="handleCheckData(record)">查看</a>
          </span>
          <span v-if="banBtn(record)" v-has="'report:edit'">
            <a-divider type="vertical"/>
            <a @click="handleEditData(record)">编辑</a>
          </span>
          <span v-if="banBtn(record)" v-has="'report:edit'">
            <a-divider type="vertical"/>
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDeleteData(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
          <span v-has="'report:back'">
            <span v-if="record.applystate === 1 || record.state === 3">
              <a-divider type="vertical"/>
              <a-popconfirm title="确定退回吗?" @confirm="() => handleSendBack(record)">
                <a>退回</a>
              </a-popconfirm>
            </span>
          </span>
          <span v-has="'report:export'">
            <a-divider type="vertical"/>
            <a @click="handleDownload(record)">下载</a>
          </span>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <creatReport ref="creatModel" @openModelView="handleAddData"></creatReport>
    <component v-bind:is="ModelView" ref="modalForm" @ok="updateProList"></component>
  </a-card>
</template>

<script>
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import store from '@/store/'
  import ModelView7 from './ModelView/ModelView7'
  import {getAction} from '@/api/manage'
  import moment from 'moment'
  import 'moment/locale/zh-cn'
  import columnsTransfer from './columnsTransfer'
  import creatReport from './creatReport'
  import {getApi} from "@/utils/neiORwai"

  export default {
    name: 'bbsb',
    mixins: [JeecgListMixin],
    components: {
      creatReport,
      ModelView7,
    },
    data() {
      return {
        queryParam: {},
        superParam: {
          type: '1',
          code: '7',
          sjType: '5',
        },
        // 表头
        columnsTransfer: columnsTransfer(this),
        url: {
          list: '/sjxxzhbb/sjAuditInfo/queryAuditorSJSOneInfo',
          downLoad: '/sjxxzhbb/sjsbb/exportDwjnbsjjgbbqkb',
          merge: '/sjxxzhbb/sjsbb/generateAuditReport',
          back: '/common/sjProcCommon/backReport',
        },
        yearPickShow: false,
        banEdit: true,
        title: '单位及内部审计机构基本情况表',
        modelNum: '7',
        reportIds: [],
        departInfo: store.getters.departInfo,
      }
    },
    computed: {
      ModelView: function () {
        return 'ModelView' + this.modelNum
      },
      rowSelection() {
        return {
          onChange: (selectedRowKeys, selectedRows) => {
            this.reportIds = []
            console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
            // selectedRows.map((item) => {
            //   this.reportIds.push(item.id)
            // })
            this.reportIds = [...selectedRowKeys]
          },
        }
      },
    },
    watch: {
      'superParam.code': {
        handler: function (val) {
          if (val) {
            this.searchList()
          }
        },
        immediate: true,
      },
    },
    methods: {
      // 查询条件
      searchList() {
        this.loadData(1)
      },

      // 年份选择器 begin
      moment,
      // 得到年份选择器的值
      handlePanelChange(value) {
        this.yearPickShow = false
        this.queryParam.year = moment(value).format('YYYY')
      },
      // 弹出日历和关闭日历的回调
      handleOpenChange(status) {
        this.yearPickShow = status
        if(this.queryParam.year){
          this.queryParam.year = moment(this.queryParam.year.slice(0, 4)).format('YYYY')
        }
      },
      // 年份选择器 end

      // 新增弹窗
      openCreatModel() {
        this.$refs.creatModel.visible = true
      },

      // 操作按钮
      handleAddData: function (record) {
        this.$refs.modalForm.add(record)
        this.$refs.modalForm.title = this.title
        this.$refs.modalForm.isCheck = false
        this.$refs.modalForm.disableSubmit = false
      },
      handleCheckData: function (record) {
        this.$refs.modalForm.check(record)
        this.$refs.modalForm.title = this.title
        this.$refs.modalForm.isCheck = true
        this.$refs.modalForm.disableSubmit = true
      },
      handleEditData: function (record) {
        this.$refs.modalForm.edit(record)
        this.$refs.modalForm.title = this.title
        this.$refs.modalForm.isCheck = false
        this.$refs.modalForm.disableSubmit = false
      },
      handleDeleteData: function (record) {
        this.$refs.modalForm.delete(record, () => {
          this.searchList()
        })
      },
      handleDownload: function (record) {
        let params = {}
        params = {
          year: record.year,
          quarter: record.quarter,
          officeId: record.officeId,
        }
        getAction(
          this.url.downLoad,
          Object.assign({id: record.id, type: this.queryParam.sjType, sjType: this.queryParam.sjType}, params)
        )
          .then((res) => {
            if (res.success && res.result) {
              this.$message.success('正在下载，请稍等！')
              let url = `${ getApi() || window._CONFIG['domianURL']}/sys/common/static${res.result}`
              window.open(url, '_blank')
            } else {
            }
          })
          .finally(() => {
          })
      },
      updateProList() {
        this.loadData(1)
      },
      banBtn(record) {
        if (record.state == 0 || record.state == 2) {
          return true
        } else {
          return false
        }
      },
      mergeReports() {
        this.loading = true
        getAction(this.url.merge, {ids: JSON.stringify(this.reportIds)})
          .then((res) => {
            this.loading = false
            if (res.success) {
              this.$message.success(res.message)
              this.loadData()
            } else {
              this.$message.error(res.message)
            }
          })
          .finally(() => {
          })
      },
      handleSendBack(record) {
        // 退回
        let url = this.url.back
        let type = this.superParam.sjType
        getAction(url, {type, proId: record.id, reason: ''}).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
          }
        })
      },
    },
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>